/****
 *
 *一个用于绘制文字的text.js
 *
 * Daniel
 *
 * 2018.05.25
 *
 ****/
define(["ajax"],function(ajax){
    
    var text = function(){
        this.world = {};
        this.scene = {};
        this.typeface = {};         //字体库
        this.targetT = [];                  //临时容器
        this.font = {                       //字体设置
            color:  0x000000,               //字体颜色
            transparent:  false,            //是否透明
            opacity:  1,                    //透明度
            scale: 20,                      //字体大小
        };                  //字体设置
        this.urlFont = "";
        this.historyData = {
            textcontainer : {},
            textarray : []
        };          //历史数据
        this.textGroup  = new THREE.Group();        //模型容器
        this.position = {};
    };
    
    //初始化各项参数
    text.prototype.init = function(world,fontUrl){
        this.world = world;
        this.scene = world.scene;
        this.urlFont = fontUrl ||"";
        this.getFont(this.urlFont[0]);
        this.textGroup.name = "textG";
        this.scene.add(this.textGroup);
        this.dict = new Dict();
    
    
    };
    
    //生成文字数据
    text.prototype.inputEnter = function(){
        if(this.position.isVector3 == undefined)return;
        var scope = this;
        var geom;
        var group = new THREE.Group();
        var name = group.uuid;
            group.name = name;
            //group.position.copy(this.position.clone());
        scope.textGroup.add(group);
        scope.dict.init(name,group);
        scope.historyData.textcontainer[name]={};
        scope.historyData.textcontainer[name].position = this.position.clone();
        scope.historyData.textcontainer[name].data = [];
        geom = scope.targetT[0].clone();
        group.add(geom);
        scope.scene.remove(scope.targetT[0]);
        
    };
    
    //取消文字生成
    text.prototype.inputCancel = function () {
        if(this.targetT.length > 0){
            var object = this.targetT[0];
            this.scene.remove(object);
            this.targetT = [];
        }
    };
    
    //文字预览
    text.prototype.viewGeom = function(){
        var scope = this;
        var scene = this.scene;
        return function(text){
            if(text == undefined)return;
            var model = scope.geometry(text);
            if(scope.targetT.length > 0){
                var object = scope.targetT[0];
                    scene.remove(object);
                    scope.targetT = [];
            }
            scene.add(model);
            scope.targetT.push(model);
        };
    };
    
    //获取字体
    text.prototype.getFont = function(url){
        var loader = new THREE.FontLoader();
        var scope = this;
        loader.load( url, function ( font ) {
            scope.typeface = font
        });
    };
    
    //更改字体
    text.prototype.changeFont = function(type){
        if(type==undefined)return;
        var fonturl = this.urlFont;
        switch (type){
            case "FZLT":
            case 0:
                this.getFont(fonturl[0]);
                break;
            case "STSong":
            case 1:
                this.getFont(fonturl[1]);
                break;
            case "YouYuan":
            case 2:
                this.getFont(fonturl[2]);
                break;
            case "LiSu":
            case 3:
                this.getFont(fonturl[3]);
                break;
            case "English":
            case 4:
                this.getFont(fonturl[4]);
                break;
            default:
                this.getFont(fonturl[0]);
                break;
        }
    
    };
    
    //生成相关模型
    text.prototype.geometry = function (message) {
        if( typeof message === 'string'){
            var font = this.typeface;
            var fonts = this.font.scale;
            var shapes = font.generateShapes(message,fonts);
            var geometry = new THREE.ShapeGeometry( shapes );
            var textShape = new THREE.BufferGeometry();
            textShape.fromGeometry(geometry);
            var matLite = new THREE.MeshBasicMaterial( {
                color: new THREE.Color(this.font.color),
                transparent: this.font.transparent,
                opacity: this.font.opacity,
                side: THREE.DoubleSide
            } );
            var text = new THREE.Mesh( textShape, matLite );
            text.position.copy(this.position.clone());
            return text;
        }
        
    };
    
    //获取文字位置
    text.prototype.getPoint = function (object){
        if(object.length >0){
            var vector = object[0].point.clone();
            this.position = vector;
            return vector;
        }
    };
    
    //清除
    text.prototype.clear = function(){
        this.textGroup.children = [];
        this.dict.clear();
        this.historyData
    };
    
    //撤销
    text.prototype.undo = function(){
        var object = this.dict.undo();
        object && this.textGroup.remove(object);
        return true;
    };
    
    //重做
    text.prototype.redo = function(){
        var object = this.dict.redo();
        object && this.textGroup.add(object);
        return true;
    };
    
    return new text();
    
});